 
import { EActionCode } from "framework/FSM/Action/ActionCode";
import { ActionBase, ActionBaseOfCreep } from "../ActionBase";
 

/**
 * 升级Action
 */
export class ActionBoostCreep  extends ActionBaseOfCreep<IActionCMD<"boostCreep">>
{
    protected updateArgs(action: IActionCMD<"boostCreep">): EActionCode 
    {
        if(super.updateArgs(action) !== EActionCode.OK)
        {
            return EActionCode.FAILED;
        }
        const creep = action.arg.entity;
        const thisroom = creep.ownerRoom();
        let boostTask = thisroom.findTaskById<ITaskDeclare["boost"]>(creep.name,"boost");
        if(!boostTask)
        {
            return EActionCode.FAILED;
        }
        action.arg.boostTask = boostTask.data.arg;
        return EActionCode.OK;
    }
    protected _execute(action: IActionCMD<"boostCreep">): ActionCode 
    {
 
        const creep = action.arg.entity;
        creep.log("info","正在boost");
        const boosttask = action.arg.boostTask;
            
        const labentity = action.arg.room.getEntity("LabEntity");
        // 资源以及准备好了。可以去强化了
        creep.log(`info`,'执行boost Action',creep.memory.IsBoosted);
        let movegoal:RoomPosition|undefined ;
        for(let i = 0;i<   boosttask.res.length;)
        {
            const lab = Game.getObjectById( labentity.inquireMinerialLab(boosttask.res[i].resource))  as StructureLab;
            if(!lab)
            {
                global.Log.Error(`数据错误 查询不到要强化的lab ${JSON.stringify(boosttask.res) }  --> ${JSON.stringify(labentity.getNodeMemory().minerialRecord)}`);
            }
            if(!movegoal)
            {
                movegoal = lab.pos;
            }
            if(lab.pos.distance(creep.pos)<=1)
            {
                if(lab.boostCreep(creep) == OK)
                {
                    boosttask.res.splice(i,1);
                    continue;
                }
            }
            i++
        }
   
        if(boosttask.res.length>0)
        {
            creep.mymoveTo({pos:movegoal as RoomPosition,range:1},{});
        }
        else
        {
            creep.memory.IsBoosted = true;
            return EActionCode.OK;
        }
        return EActionCode.RUNNING;
    }
}